package cn.ad.server.web.spring.service.httpclient4;

import cn.ad.server.web.spring.service.components.http.client.AdResponseHandler;
import cn.ad.server.web.spring.service.components.http.client.ResponseHandleException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * 处理响应返回string
 * @author wangwei
 *
 */
public class AdBasicResponseHandler implements AdResponseHandler<String> {
	
	private static Logger LOGGER = LoggerFactory.getLogger(AdBasicResponseHandler.class);
	
	private ResponseHandler<String> handler;
	
	@Override
	public String doHandle(Object response) throws ResponseHandleException {
		if(response==null){
			throw new ResponseHandleException("处理响应的资源不存在。");
		}
		if(response instanceof HttpResponse){			
			try {
				return handler.handleResponse((HttpResponse)response);
			} catch (IOException e) {
				throw new ResponseHandleException("handle Response error :" + e.getMessage(),e.getCause());
			}
		}
		throw new ResponseHandleException("The Request is not support. class:" + response.getClass().getName());
	}

	public ResponseHandler<String> getHandler() {
		return handler;
	}

	public void setHandler(ResponseHandler<String> handler) {
		this.handler = handler;
	}

	

}
